package com.share.wechat.core.service.impl;

import com.share.wechat.core.config.constant.SystemConfig;
import com.share.wechat.core.entity.ConfigSystem;
import com.share.wechat.core.repository.inter.ConfigSystemRepository;
import com.share.wechat.core.service.ConfigSystemService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Set;

@Service
public class ConfigSystemServiceImpl implements ConfigSystemService {
    private final Logger logger = Logger.getLogger(this.getClass());
    @Resource
    private ConfigSystemRepository configSystemRepository;

    @Override
    public Set<ConfigSystem> findByParentCode(String parentCode) {
        logger.info("查询的分组信息为" + parentCode);
        Set<ConfigSystem> sets = configSystemRepository.findByParentCode(parentCode);
        logger.info("查询到的数据为" + sets);
        return sets;
    }

    @Override
    public String getAppId() {
        Set<ConfigSystem> sets = configSystemRepository.findByParentCode(SystemConfig.APP_ID);
        if (!CollectionUtils.isEmpty(sets)) {
            ConfigSystem config = sets.iterator().next();
            logger.info("参数为" + SystemConfig.APP_ID + "查询到的数据为" + config);
            return config.getKeyCode();
        }

        return null;
    }

    @Override
    public String getAppSecret() {
        Set<ConfigSystem> sets = configSystemRepository.findByParentCode(SystemConfig.APP_SECRET);
        if (!CollectionUtils.isEmpty(sets)) {
            ConfigSystem config = sets.iterator().next();
            logger.info("参数为" + SystemConfig.APP_SECRET + "查询到的数据为" + config);
            return config.getKeyCode();
        }
        return null;
    }
}
